﻿'准备引用 无赖 类的偷东西事件  
'Public WithEvents w As 无赖
'Sub Main()
'    Console.WriteLine("新的一天开始了....")
'    ' 实例华警察、无赖、自愿反扒队 类  
'    Dim p As New 警察
'    w = New 无赖
'    Dim pp As New 自愿反扒队
'    '设置对无赖类的偷东西事件的监视，并设置相应的事件处理程序  
'    '如果小偷玩偷东西则引用警察的罚钱处理程序  
'    AddHandler 无赖.PlayGame, AddressOf p.扣钱
'    '如果小偷偷东西则引用自愿反扒队的打人处理程序  
'    AddHandler 无赖.PlayGame, AddressOf pp.打人
'    w.偷东西()
'    Console.WriteLine("天黑了....")
'    Console.ReadLine()
'End Sub
'End Class  

'定义委托类型  
'Delegate Sub PlayGameHandler(ByVal sender As Object, ByVal e As System.EventArgs)

Public Class 警察
    Sub New()
        Console.WriteLine("生成警察......")
    End Sub
    '定义的事件处理程序的参数类型要与定义的委托的参数类型相一致  
    Public Sub 扣钱(ByVal sender As Object, ByVal e As System.EventArgs)

        Console.WriteLine("警察：好小子，胆敢偷东西...")
        '定义线程输出迟缓3秒  
        System.Threading.Thread.Sleep(3000)
        Console.WriteLine("警察：看看你小子有多少钱...")
        Dim f As 无赖 = sender
        System.Threading.Thread.Sleep(3000)
        Console.WriteLine("无赖 的钱： " + f.钱.ToString())
        System.Threading.Thread.Sleep(3000)
        Console.WriteLine("开始扣钱......")
        System.Threading.Thread.Sleep(3000)
        f.钱 = f.钱 - 500
        Console.WriteLine("扣完了....现在无赖 还剩下：" + f.钱.ToString())
        System.Threading.Thread.Sleep(3000)
    End Sub

End Class

Public Class 无赖
    Delegate Sub PlayGameHandler(ByVal sender As Object, ByVal e As System.EventArgs)

    '先定义一个偷东西的事件使用现有委托类型 PlayGameHandler为基础委托  
    Public Event PlayGame As PlayGameHandler
    Private m_Money As Integer
    Private ey As Integer
    Sub New()
        Console.WriteLine("生成无赖....")
        m_Money = 1000
        ey = 5
    End Sub
    '定义挨打属性用来记录挨打过几次  
    Property 挨打() As Integer
        Get
            Return ey
        End Get
        Set(ByVal Value As Integer)
            ey = Value
        End Set
    End Property
    '定义前属性用来记录有多少钱  
    Property 钱() As Integer
        Get
            Return m_Money
        End Get
        Set(ByVal Value As Integer)
            m_Money = Value
        End Set
    End Property
    '定义偷东西过程  
    Public Sub 偷东西()
        Console.WriteLine("无赖开始偷东西了")
        System.Threading.Thread.Sleep(3000)
        '实例化事件参数  
        Dim e As New System.EventArgs
        '引发偷东西事件，参数类型要与现有委托类型一致  
        RaiseEvent PlayGame(Me, e)
    End Sub
End Class

Public Class 自愿反扒队
    Sub New()
        Console.WriteLine("自愿反扒队来了.....")
        System.Threading.Thread.Sleep(3000)
    End Sub
    '定义的事件处理程序的参数类型要与定义的委托的参数类型相一致  
    Public Sub 打人(ByVal sender As Object, ByVal e As System.EventArgs)
        System.Threading.Thread.Sleep(3000)
        Console.WriteLine("自愿反扒队：好小子，敢偷东西...")
        System.Threading.Thread.Sleep(3000)
        Console.WriteLine("自愿反扒队：你小子挨过几次打了...")
        Dim bn As 无赖 = sender
        System.Threading.Thread.Sleep(3000)
        Console.WriteLine("无赖：" + bn.挨打.ToString + "次")
        System.Threading.Thread.Sleep(3000)

        Console.WriteLine("自愿反扒队：那也得揍你，先来一拳.... ")
        System.Threading.Thread.Sleep(3000)
        Console.WriteLine("自愿反扒队：再来一脚......")
        System.Threading.Thread.Sleep(3000)

        Console.WriteLine("自愿反扒队：打完了...赶紧去医院吧...")
    End Sub
End Class

